package self.zhanpeng.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import java.io.File;

@Configuration
public class StaticConfigurer implements WebMvcConfigurer {
    private static final Logger logger = LoggerFactory.getLogger(StaticConfigurer.class);

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        File jarfile = applicationHome.getSource();

        String static_path = jarfile.getParent().toString() + "/static";

        File dir = new File(static_path);

        if(!dir.exists() && !dir.isDirectory())
        {
            logger.warn("[static]:" + static_path + " is not existed. create it.");
            dir.mkdir();
        }

        logger.info("[static]:" + static_path);

        registry.addResourceHandler("/**").addResourceLocations("file://" + static_path + '/');

        logger.debug("");
    }
}
